<template>
  <div class="article">
    <div class="article_memu">
      <div class="article_memu_header">
        <div class="title">
          {{ goodsCateTitle }}
        </div>
      </div>
      <el-card shadow="never">
        <el-tree
          :data="treeData"
          :props="props"
          :expand-on-click-node="false"
          :highlight-current="highlightCurrent"
          @node-click="handleNodeClick"
        />
        <div class="btn-info" style="margin-top:20px">
          <el-button style="width:100%" type="primary" @click="visible = true">维护模块</el-button>
        </div>
      </el-card>
    </div>
    <div style="width:100%">
      <el-button style="margin-bottom:10px" type="primary" @click="addGoods">新增类目</el-button>

      <el-table
        ref="multipleTable"
        v-loading="listLoading"
        :data="list"
        height="calc(100vh - 200px)"
        style="width: 100%;"
        size="mini"
        border
        tooltip-effect="dark"
        :header-cell-style="{ background: '#eaf1fd', color: '#333' }"
      >
        <el-table-column
          label="类目名称"
        >
          <template slot-scope="{ row }">
            <div>
              <span>{{ row.name }}</span>
            </div>
          </template>

        </el-table-column>
        <el-table-column
          label="类目编码"
        >
          <template slot-scope="{ row }">
            <div>
              <span>{{ row.code }}</span>
            </div>
          </template>

        </el-table-column>
        <el-table-column
          label="类目图片"
          width="200"
        >
          <template slot-scope="{ row }">
            <div>
              <img style="width:100px" :src="row.image" alt="">
            </div>
          </template>

        </el-table-column>
        <el-table-column
          label="状态"
          width="200"
        >
          <template slot-scope="{ row }">
            <div>
              <span v-if="row.status == 1" style="color:blue">启用</span>
              <span v-if="row.status == 2" style="color:red">禁用</span>

            </div>
          </template>

        </el-table-column>
        <el-table-column
          label="操作"
          align="right"
          width="180"
          class-name="small-padding fixed-width"
          fixed="right"
        >
          <template slot-scope="{ row }">
            <el-button
              size="mini"
              type="primary"
              @click="edit(row)"
            >
              编辑
            </el-button>
            <el-button
              type="danger"
              @click="delet(row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </div>
    <goodsType :visible="visible" :type-list="treeData" @update="closeDraw" @reLoad="getCateList" />

    <el-dialog
      title="类目"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <div style="padding: 20px">
        <el-cascader
          v-model="typeChooseList"
          style="width:100%;margin:10px 0"
          :options="treeData"
          :props="{ label: 'name',
                    value: 'id',
                    children: 'children' }"
          clearable
          placeholder="请选择所属分类"
        />
        <el-input
          v-model="typeForm.name"
          style="width:100%;margin-bottom:10px"
          placeholder="请输入类目名称"
        />
        <el-input v-model="typeForm.code" style="width:100%;margin-bottom:10px" placeholder="请输入类目编码" />
        <el-select
          v-model="typeForm.status"
          style="width:100%"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            label="启用"
            :value="1"
          />
          <el-option
            label="禁用"
            :value="2"
          />
        </el-select>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button v-if="dialogType == 1" type="primary" @click="submitAdd()">确 定</el-button>
        <el-button v-if="dialogType == 2" type="primary" @click="submitEdit()">保 存</el-button>

      </span>
    </el-dialog>

  </div>

</template>

<script>
import { goodsListCate, goodsList, addGoodsCategory, delGoodsCategory, updateGoodsCategory } from '@/api/base/goods'
import Pagination from '@/components/Pagination'
import goodsType from './components/goodsType.vue'

export default {
  components: {
    Pagination,
    goodsType
  },
  data() {
    return {
      treeData: [],
      props: {
        label: 'name',
        children: 'children'
      },
      goodsCateTitle: '全部商品',
      highlightCurrent: true,

      list: null,
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 20,
        cate: ''

      },
      visible: false,
      dialogVisible: false,
      typeChooseList: [],
      typeForm: {
        name: '',
        cate1: '',
        cate2: '',
        status: '',
        code: '',
        image: ''
      },
      dialogType: 1,
      data: {}
    }
  },
  mounted() {
    this.getCateList()
    // this.getList('')
  },
  methods: {
    addGoods() {
      this.typeForm = {
        name: '',
        cate1: '',
        cate2: '',
        status: '',
        code: '',
        image: ''
      }
      this.dialogType = 1
      this.dialogVisible = true
    },
    closeDraw() {
      this.visible = false
    },
    getCateList() {
      goodsListCate().then(res => {
        this.treeData = res.data.list
      })
    },
    handleNodeClick(data) {
      console.log(data)
      if (data.pid === 0) {
        this.typeChooseList = [data.id]
      } else {
        this.typeChooseList = [data.pid, data.id]
      }
      this.data = data
      this.goodsCateTitle = data.name
      this.listQuery.cate = data.id
      this.listQuery.page = 1
      this.getList()
    },
    getList(id) {
      this.listLoading = true
      goodsList(
        this.listQuery
      ).then(res => {
        this.list = res.data.list
        this.total = res.data.count
        this.listLoading = false
      })
    },
    edit(row) {
      this.typeChooseList = []
      this.typeChooseList.push(row.cate1, row.cate2)
      this.typeForm = {
        name: row.name,
        cate1: row.cate1,
        cate2: row.cate2,
        status: row.status,
        code: row.code,
        image: row.image,
        id: row.id
      }
      this.dialogType = 2
      this.dialogVisible = true
    },
    delet(row) {
      this.$confirm('此操作将删除该类目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delGoodsCategory({
          id: row.id
        }).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList(this.data.id)
        })
      }).catch(() => {

      })
    },
    submitAdd() {
      var that = this
      if (!this.typeChooseList.length) return this.$message.error('请选择分类')
      this.typeChooseList.push('')
      this.typeForm.cate1 = this.typeChooseList[0]
      this.typeForm.cate2 = this.typeChooseList[1]
      addGoodsCategory(this.typeForm).then(res => {
        this.$message.success('添加成功')
        that.getList(that.data.id)
        this.dialogVisible = false
      })
    },
    submitEdit() {
      var that = this
      if (!this.typeChooseList.length) return this.$message.error('请选择分类')
      this.typeChooseList.push('')
      this.typeForm.cate1 = this.typeChooseList[0]
      this.typeForm.cate2 = this.typeChooseList[1]
      updateGoodsCategory(this.typeForm).then(res => {
        this.$message.success('编辑成功')
        that.getList(that.data.id)
        this.dialogVisible = false
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.article{
  display: flex;
  padding: 20px;
  .article_memu{
    width: 200px;
    margin-right:20px;
    .article_memu_header{
      text-align: center;
      background-color: #1890ff;
      color: #fff;
      font-size: 16px;
      padding: 10px;
      border-radius: 5px;
      margin-bottom: 10px;
      cursor: pointer;
    }
  }
}
</style>
